package com.syqc.choose.team

import android.app.Activity
import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.syqc.adapter.ChooseTeamAdapter
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.setResultTo
import com.syqc.comlib.utils.toast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentChooseCarBinding
import com.syqc.monitor.databinding.IncludeChooseTeamCarBinding
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 *@Author Jasper
 *@Time  2021/5/14 15:59
 *@Desc
 */
class ChooseTeamFragment :
    LazyFragment<FragmentChooseCarBinding>(FragmentChooseCarBinding::inflate) {
    private var isMap: Boolean = false
    private var isChoose: Boolean = false

    private lateinit var mTeamVB: IncludeChooseTeamCarBinding

    private val mAdapter by lazy { ChooseTeamAdapter() }

    private val mViewModel by activityViewModels<ChooseTeamViewModel>()

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        if (activity is ChooseTeamActivity) {
            isMap = activity.intent.getBooleanExtra("isMap", false)
            isChoose = activity.intent.getBooleanExtra("isChoose", false)
            mAdapter.isChoose = isChoose
        }
    }

    //空布局
    private val emptyView by lazy {

        val view=    layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.includeRecycler.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(),R.mipmap.ic_empty)
        view
    }

    override fun initView(view: View) {
        mTeamVB = IncludeChooseTeamCarBinding.bind(binding.root)
        initRecyclerView()
    }

    override fun initData() {
        mViewModel.getAllTeam()
        mViewModel.toastObserver().observeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe(::toastObserver)
        mViewModel.teamTitleSub.observeOn(AndroidSchedulers.mainThread()).autoDispose(scopeProvider)
            .subscribe { mTeamVB.tvTeamName.text = it }

        mViewModel.teamsSub.hide().observeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe {
                if (it.size <= 0) {
                    mAdapter.setEmptyView(emptyView)
                    return@subscribe
                }
                mAdapter.setList(it)
            }

        mViewModel.isRootTeam.observe(this) {
            mTeamVB.tvTeamBack.isVisible=!it
            mTeamVB.tvTeamHome.isVisible=!it
        }
    }

    override fun initEvent() {
        mTeamVB.tvTeamBack.setOnClickListener {
            mViewModel.previousTeam()
        }
        mTeamVB.tvTeamHome.setOnClickListener {
            mViewModel.rootTeam()
        }
    }


    private fun initRecyclerView() {
        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        mAdapter.setEmptyView(emptyView)
        binding.includeRecycler.recyclerView.adapter = mAdapter.apply {
            setOnItemClickListener { adapter, view, position ->
                mViewModel.getTeamsById(data[position].teamId)
            }
            setOnItemChildClickListener { adapter, view, position ->
                if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
                val teamInfo = data[position]
                if (!isChoose && teamInfo.carNum <= 0) {
                    R.string.toast_choose_no_car.toast(requireContext())
                    return@setOnItemChildClickListener
                }
                if (isMap && teamInfo.carNum > 500) {
                    R.string.toast_team_cars_much.toast(requireContext())
                    return@setOnItemChildClickListener
                }
                setResultTo(requireActivity()) {
                    putExtra(Keys.INTENT_TEAM_ID, teamInfo.teamId)
                    putExtra(Keys.INTENT_TEAM_NUM, teamInfo.carNum.toString())
                    putExtra(Keys.INTENT_TEAM_NAME, teamInfo.teamName)
                }
                requireActivity().finish()
            }
        }
    }
}